﻿using System;

namespace ComponentSystem
{
    /// <summary>
    /// 组件工厂
    /// </summary>
    public static class ComponentFactory
    {
        #region 创建组件

        /// <summary>
        /// 创建组件，挂载在父组件
        /// </summary>
        public static Component CreateWithParent(Type type, Component parent)
        {
            Component component = (Component)Activator.CreateInstance(type);
            component.Parent = parent;
            ComponentWithId componentWithId = component as ComponentWithId;
            if (componentWithId != null)
            {
                componentWithId.InstanceId = component.InstanceId;
            }

            return component;
        }

        /// <summary>
        /// 创建组件，挂载在父组件
        /// </summary>
        public static T CreateWithParent<T>(Component parent) where T : Component
        {
            Type type = typeof(T);
            Component component = (Component)Activator.CreateInstance(type);
            component.Parent = parent;
            ComponentWithId componentWithId = component as ComponentWithId;
            if (componentWithId != null)
            {
                componentWithId.InstanceId = component.InstanceId;
            }

            return component as T;
        }

        /// <summary>
        /// 创建组件
        /// </summary>
        public static T Create<T>() where T : Component
        {
            Type type = typeof(T);
            Component component = (Component)Activator.CreateInstance(type);
            ComponentWithId componentWithId = component as ComponentWithId;
            if (componentWithId != null)
            {
                componentWithId.InstanceId = component.InstanceId;
            }

            return component as T;
        }

        /// <summary>
        /// 创建组件，并设置实例ID
        /// </summary>
        public static T CreateWithId<T>(long id) where T : ComponentWithId
        {
            Type type = typeof(T);
            ComponentWithId component = (ComponentWithId)Activator.CreateInstance(type);
            component.InstanceId = id;

            return component as T;
        }


        #endregion

        #region 创建实体

        /// <summary>
        /// 创建实体
        /// </summary>
        public static T CreateEntityCSharp<T>() where T : EntityCSharp
        {
            Type type = typeof(T);
            EntityCSharp component = (EntityCSharp)Activator.CreateInstance(type);

            return component as T;
        }

        /// <summary>
        /// 创建实体，并设置实例ID
        /// </summary>
        public static T CreateEntityCSharpWithId<T>(long id) where T : EntityCSharp
        {
            Type type = typeof(T);
            EntityCSharp component = (EntityCSharp)Activator.CreateInstance(type);
            component.InstanceId = id;

            return component as T;
        }
        /// <summary>
        /// 创建实体，并设置父Mono实体
        /// </summary>
        public static T CreateEntityCSharpByMono<T>(EntityMono parentMono) where T : EntityCSharp
        {
            EntityCSharp entity = CreateEntityCSharp<T>();
            entity.UpdateParentMono(parentMono);
            return entity as T;
        }

        #endregion
    }
}
